$(document).ready(function(){

	"use strict";

	/* CONFIG ================================================================ */

	var config = {
		
	}

	/* TEMPLATE ================================================================ */

	var template = {

		init: function(){
			this.header.init();
			this.page.init();
			this.modules.parallax.init();
			this.features.smoothScroll.init();
			this.features.pageTopLink.init();
		},

		/* HEADER -------------------------------- */

		header: {
			element: $("#header"),

			minHeaderHeight: 40,
			maxHeaderHeight: 80,

			init: function(){
				this.maxHeaderHeight = this.element.height();
			},

			updateHeight: function(){
				var v = Math.round(this.maxHeaderHeight - Math.max(0, Math.min(this.maxHeaderHeight - this.minHeaderHeight, $(window).scrollTop()*0.5 - 0)));

				/* handle header height */
				this.element.height(v);
				$("#logo a img").height(v);
				$("#main-menu li a").css("line-height", v + "px");
			}
		},

		/* PAGE -------------------------------- */

		page: {
			init: function(){
				/* handle page scroll */
				$(window).bind("scroll", this.scroll);
			},

			scroll: function(){
				/* update header height */
				template.header.updateHeight();

				/* update footer position */
				template.footer.updatePosition();

				/* update page-top-link visibility */
				template.features.pageTopLink.updateVisibility();

				/* handle parallax modules */
				template.modules.parallax.updateModules();
			},

			scrollTo: function(target){
				TweenLite.to($(window), 1, {scrollTo: {y:target}, ease: "easeInOutCubic"});
			}
		},

		/* FOOTER -------------------------------- */

		footer: {
			element: $("#footer"),

			init: function(){
				
			},

			updatePosition: function(){
				this.element.css("top", 0.5*($(window).scrollTop() - $(document).height() + $(window).height()));
			}
		},

		/* MODULES -------------------------------- */

		modules: {
			parallax: {
				elements: false,

				init: function(){
					this.elements = $(".parallax");
				},

				updateModules: function(){
					if(this.elements){
						this.elements.each(function(){
							var v = Math.min(0, $(this).offset().top - $(window).scrollTop() - $(window).height()),
								parallax = $(this).data("parallax_speed") || 0.4,
								offset = ($(this).data("parallax_offset") || 0) + $(window).height(),
								direction = $(this).data("parallax_reverse") ? 1 : -1;
							$(this).css({
								"background-position": "0 " + direction*((v + offset)*parallax) + "px"
							})
						});
					}
				}
			}
		},

		/* FEATURES -------------------------------- */
		
		features: {

			/* SMOOTH SCROLL ----- */

			smoothScroll: {
				duration: 0.5,
				step: 200,

				init: function(){
					$(window).on("mousewheel DOMMouseScroll", {ss: this}, this.mouseWheel);
				},

				mouseWheel: function(e){
					var ss = e.data.ss,
						delta = e.originalEvent.wheelDelta/120 || -e.originalEvent.detail/3,
						from = $(window).scrollTop(),
						to = from - parseInt(delta*ss.step);

					e.preventDefault();

					TweenMax.to($(window), ss.duration, {scrollTo: {y: to, autoKill:true}, ease: Cubic.easeOut, autoKill: true, overwrite: 5, onUpdate: function(){template.page.scroll();}});
				}
			},

			/* PAGE TOP LINK ----- */

			pageTopLink: {
				element: $("#page-top-link"),

				active: false,

				init: function(){
					this.element.click(function(){
						template.page.scrollTo(0);
						return false;
					});

					this.element.css({
						"opacity": 0,
						"bottom": -35
					});
				},

				updateVisibility: function(){
					if($(window).scrollTop() < 150){
						this.hide();
					}else{
						this.show();
					}
				},

				show: function(){
					if(this.active) return;

					this.active = true;

					TweenLite.to($("#page-top-link"), 0.4, {opacity: 1, ease: "easeInOutCubic"});
					TweenLite.to($("#page-top-link"), 0.4, {bottom: 0, ease: "easeInOutCubic"});
				},

				hide: function(){
					if(!this.active) return;

					this.active = false;
					
					TweenLite.to($("#page-top-link"), 0.25, {opacity: 0, ease: "easeInCubic"});
					TweenLite.to($("#page-top-link"), 0.25, {bottom: -35, ease: "easeInCubic"});
				}
			}
		}

	}

	/* INIT TEMPLATE ================================================================ */

	template.init();

});